<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <#assign base=request.contextPath  />
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link rel="stylesheet" href="${base}/frame/layui/css/layui.css?t=1534970980648" media="all">
    <script src="${base}/frame/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${base}/js/zhang.js"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="${base}/js/ztree/jquery.ztree.exedit.js"></script>
    <link rel="stylesheet" href="${base}/css/demo.css" type="text/css">
    <link rel="stylesheet" href="${base}/css/zTreeStyle.css" type="text/css">
    <style type="text/css">
        form {
            padding: 10px;
        }
        .layui-btn-radius {
            border-radius: 3px;
        }
    </style>
</head>
<body>
<form class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input name="username" id="username" lay-verify="required" autocomplete="off" class="layui-input sNull"
                       type="text">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input name="password" id="password" lay-verify="required" autocomplete="off" class="layui-input "
                       type="text">
            </div>

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色名</label>
            <div class="layui-input-inline">
                <input name="roleName" id="roleName" lay-verify="required" autocomplete="off" class="layui-input "
                       type="text">
            </div>

        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-form-item">
            <label class="layui-form-label">用户</label>
            <div class="layui-input-inline">
                <select id="name" lay-verify="required" class="isNull">
                    <option value="" selected>请选择...</option>

                </select>
            </div>

        </div>
    </div>

<#if yesAdmin.username=="admin">

</#if>
    <#if yesAdmin.username!="admin">
<div class="layui-form-item">
    <div style="height: 300px;width:450px;">
        <label class="layui-form-label">选择该角色可见菜单</label>
        <ul id="tree" class="ztree"
            style="width:300px; height: 280px; overflow:auto;border:1px solid #F0F9FF;background-color: #F0F9FF;"></ul>
    </div>
</div>
    </#if>



    <div class="layui-form-item">
        <div style="height: 300px;width:450px;">
            <label class="layui-form-label">选择该角色管理小区</label>
            <ul id="tree1" class="ztree"
                style="width:300px; height: 280px; overflow:auto;border:1px solid #F0F9FF;background-color: #F0F9FF;"></ul>
        </div>
    </div>

    <div class="layui-form-item" style="margin-left: 30px">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo" lay-submit="" type="button"
                    id="onInsert" style="background-color: #0092EF;position: relative; left: 50%;">立即提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary" style="position: relative; left: 50%;">重置</button>
        </div>
    </div>
    </div>
</form>

<script>

    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;



        $.ajax({
            type: "get", //提交方式
            url: "${base}/admin/user/selectUser",//路径
            dataType: "json",
            async: false,
            success: function (data) {//返回数据根据结果进行相应的处理
                for (var i = 0; i < data.length; i++) {
                    if(data[i].detailedFloor==null){
                        $("#name").append(
                                ' <option value="' + data[i].openid + '"  >' + data[i].nickName + ''+''+'-------'+'暂无绑定'+'</option>'
                        )
                    }else{
                        $("#name").append(
                                ' <option value="' + data[i].openid + '"  >' + data[i].nickName + ''+''+'-------'+data[i].detailedFloor+'</option>'
                        )
                    }

                }
                form.render(); //更新全部
            }
        });



    });
<#if yesAdmin.username=="admin">

</#if>
    <#if yesAdmin.username!="admin">

    var zTreeObj,
            setting = {
                view: {
                    selectedMulti: false,
                    showLine: true,
                    dblClickExpand: false
                },
                callback: {
                    //回调函数 默认展开节点
                    onAsyncSuccess:
                            function () {
                                var treeObj = $.fn.zTree.getZTreeObj("tree");
                                treeObj.expandAll(true);
                            }
                },
                async: {
                    enable: true,
                    url: "${base}/admin/menu/findMenus"
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pId",
                        rootPid: 0
                    }
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    radioType: "all",
                    chkboxType: {"Y": "ps", "N": "ps"}
                }

            };
    //初始化树
    zTreeObj = $.fn.zTree.init($("#tree"), setting);
    </#if>



    var zTreeObj1,
            setting = {
                view: {
                    selectedMulti: false,
                    showLine: true,
                    dblClickExpand: false
                },
                callback: {
                    //回调函数 默认展开节点
                    onAsyncSuccess:
                            function () {
                                var treeObj = $.fn.zTree.getZTreeObj("tree1");
                                treeObj.expandAll(true);
                            }
                },
                async: {
                    enable: true,
                    url: "${base}/admin/quarters/llist"
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pId",
                        rootPid: 0
                    }
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    radioType: "all",
                    chkboxType: {"Y": "ps", "N": "ps"}
                }

            };
    //初始化树
    zTreeObj1 = $.fn.zTree.init($("#tree1"), setting);


    //添加
    $("#onInsert").one("click",function(){

        if($("#username").val()==""){
            layer.msg("用户名不能为空")
            return;
        }
        if($("#password").val()==""){
            layer.msg("密码不能为空")
            return;
        }
        if($("#roleName").val()==""){
            layer.msg("角色名不能为空")
            return;
        }
        if($("#name").val()==""){
            layer.msg("请选择微信用户")
            return;
        }
        <#if yesAdmin.username=="admin">

        </#if>
        <#if yesAdmin.username!="admin">
if(getAllID("tree")==""){
    layer.msg("请选择该角色可见菜单！");
    return;
}
        </#if>

        if(getAllID("tree1")==""){
            layer.msg("请选择该角色管理小区！");
            return;
        }


        var requestJson = {};
        <#if yesAdmin.username=="admin">

        </#if>
        <#if yesAdmin.username!="admin">
        requestJson.menuId = getAllID("tree");

        </#if>
        requestJson.quartersId = getAllID("tree1");
        requestJson.username = $("#username").val();
        requestJson.password = $("#password").val();
        requestJson.roleName = $("#roleName").val();
        requestJson.openId = $("#name").val();
        $.ajax({
            type: "POST", //提交方式
            url: "${base}/admin/userInfo/saveUserInfo",//路径
            dataType: "json",
            data: requestJson,
            success: function (data) {//返回数据根据结果进行相应的处理
                if (data.code === 0) {
                    layer.msg(data.msg);
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }, 300)
                    var table = parent.layui.table;
                    //表刷新方法
                    table.reload('testReload', {
                        url: '${base}/admin/userInfo/selectMyPage'
                        , where: {} //设定异步数据接口的额外参数
                    });
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
</script>
</body>
</html>
